﻿using FastCloud.Base;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.ORG
{
    [Route("frame/org/tenant")]
    public class TenantController : BaseController
    {
        [AutoWired]
        private DB_Tenant DB_Tenant;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/ORG/Tenant/Tenant_List.cshtml");
        }

        [Route("contact")]
        public IActionResult Contact()
        {
            return View("~/Views/ORG/Tenant/TenantContact_List.cshtml");
        }

        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/ORG/Tenant/Tenant_Add.cshtml");
        }

        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/ORG/Tenant/Tenant_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string TenantName = ValueConvert.ConvertToString(GetFormParam("TenantName"));
            string sql = "1=1";
            if (!string.IsNullOrEmpty(TenantName))
            {
                sql += " and TenantName like @TenantName";
            }
            DB_Tenant.Add2Parameter("TenantName", "%" + TenantName + "%");
            return Result.OK(DB_Tenant.SelectPage("*", sql, "SortNum desc"));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Tenant Entity)
        {
            if (DB_Tenant.ExistTenant(Entity.TenantName))
            {
                return Result.Error("已存在相同名称的租户！");
            }
            else
            {
                DB_Tenant.Insert(Entity);
                return Result.OK();
            }
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Tenant Entity)
        {
            if (DB_Tenant.ExistTenant(Entity.TenantID, Entity.TenantName))
            {
                return Result.Error("已存在相同名称的租户！");
            }
            else
            {
                DB_Tenant.UpdateByID(Entity);
                return Result.OK();
            }
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string TenantID = ValueConvert.ConvertToString(GetFormParam("TenantID"));
            return Result.OK(DB_Tenant.SelectOne(TenantID));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            DB_Tenant.DeleteBatch(KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
            return Result.OK();
        }
    }
}
